﻿using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using PetaPoco;

namespace CNF.Common.Base.SimpleOrm.DataBaseProvidrer
{
    public class CallContextDataBaseStoreage : IDataBaseStoreage<Database>
    {
        protected const string ItemContextKey = "CallContextDataBaseStoreage";

        protected Database CreateNewDataBaseByKey(string key)
        {
            return new Database(key);
        }

        public Database GetCurentDataBaseByKey(string key)
        {
            if (CallContext.GetData(ItemContextKey)==null)
            {
                CallContext.SetData(ItemContextKey, new Dictionary<string, Database>());
            }

            Dictionary<string, Database> databases = CallContext.GetData(ItemContextKey) as Dictionary<string, Database>;

            if (!databases.ContainsKey(key))
            {
                databases.Add(key, CreateNewDataBaseByKey(key));
            }

            return databases[key];
        }
    }
}
